package net.toocruel.iqismart.web.controller.common;

import net.toocruel.iqismart.dao.condition.TagCondition;
import net.toocruel.iqismart.dao.core.TagRepository;
import net.toocruel.iqismart.entity.common.Tag;
import net.toocruel.iqismart.entity.core.Message;
import net.toocruel.iqismart.entity.core.Notification;
import net.toocruel.iqismart.security.core.support.Result;
import net.toocruel.iqismart.service.core.MessageService;
import net.toocruel.iqismart.service.core.NotificationService;
import net.toocruel.iqismart.service.core.QuestionService;
import net.toocruel.iqismart.service.core.TagService;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/6 上午4:38
 */
@Controller
@RequestMapping("ajax")
public class AjaxController extends AbstractBaseController {

    @Autowired
    private TagService tagService;

    @Autowired
    private TagRepository tagRepository;

    @Autowired
    private NotificationService notificationService;

    @RequestMapping("loadTags")
    @ResponseBody
    public Page<Tag> loadCities(String word,Long categoryId,
                                @RequestParam(defaultValue = "14") int size,
                                @RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "id") String sortBy,
                                @RequestParam(defaultValue = "desc") String sortOrder){
        try {

//            tagRepository.save(new Tag("aaa"));
//            tagRepository.save(new Tag("bbb"));
//            tagRepository.save(new Tag("ccc"));
//            tagRepository.save(new Tag("ddd"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));
//            tagRepository.save(new Tag("eee"));

            Page<Tag> tags = tagService.query(TagCondition.withNameStartAndCategoryId(word,categoryId),new PageRequest(page-1, size,new Sort(Sort.Direction.fromString(sortOrder),sortBy)));



            return tags;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @RequestMapping("unreadNotifications")
    @ResponseBody
    public String unreadNotifications(){
        List<Notification> list = notificationService.findByToUser(getCurrentUser().getId());
        if(CollectionUtils.isNotEmpty(list)){
            return "<span class=\"fa fa-bell-o fa-lg\"></span><span class=\"label label-danger\">"+list.size()+"</span>";
        }else{
            return "<span class=\"fa fa-bell-o fa-lg\"></span>";
        }

    }

    @Autowired
    private MessageService messageService;
    @RequestMapping("unreadMessages")
    @ResponseBody
    public String unreadMessages(){
        List<Message> list = messageService.findByToUser(getCurrentUser().getId());
        if(CollectionUtils.isNotEmpty(list)){
            return "<span class=\"fa fa-envelope-o fa-lg\"></span><span class=\"label label-success\">"+list.size()+"</span>";
        }else{
            return "<span class=\"fa fa-envelope-o fa-lg\"></span>";
        }
    }

    ///ajax/loadInviteUsers?question_id=11&word=1
    @Autowired
    private QuestionService questionService;
    @RequestMapping("loadInviteUsers")
    @ResponseBody
    public Result loadInviteUsers(Long questionId,String word){
        Result re = questionService.getRecommentInviteUsers(questionId,word);
        return re;
    }

    //question/11/invitations/part
//    @RequestMapping("unreadNotifications")
//    @ResponseBody
//    public String unreadNotifications(){
//        return "";
//    }

}
